package com.example.a66666.yuejian.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.a66666.yuejian.Bean.RegisterBean;
import com.example.a66666.yuejian.Listener.RetrofitListener;
import com.example.a66666.yuejian.Model.UserModel;
import com.example.a66666.yuejian.R;

public class RegisterActivity extends AppCompatActivity {

    private EditText username,password;
    private Button doregister,back;
    private Intent intent1,intent2;
    private RetrofitListener<RegisterBean> listener;
    private UserModel model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initClick();
        initListener();


    }

    public void initView(){
        username=(EditText)findViewById(R.id.edit_username);
        password=(EditText)findViewById(R.id.edit_password);
        doregister=(Button)findViewById(R.id.btn_doregister);
        back=(Button)findViewById(R.id.btn_back);
    }

    public void initClick(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent1=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent1);
            }
        });

        doregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model=new UserModel();
                model.userRegister(username.getText().toString(),password.getText().toString(),listener);
            }
        });
    }

    public void initListener(){
        listener=new RetrofitListener<RegisterBean>() {
            @Override
            public void onSuccess(RegisterBean registerbean) {
                if("1".equals(registerbean.getFlag())){
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                    intent2=new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent2);
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_LONG).show();
            }
        };
    }
}
